﻿using System;
using System.Diagnostics;
using VXCompany.SilverlightLogging.Services.Exceptions.LoggingSinks;
using System.ComponentModel.Composition;

namespace VXCompany.SilverlightLogging.AppFabricSink
{
	/// <summary>
	/// Sample Logging Sink implementation.
	/// AppFabric Exception Sink. Will only work on Windows 7 or 2008, with IIS7 and AppFabric.
	/// </summary>
	[Export(typeof(ILoggingSink))]
	public class AppFabricLoggingSink : ILoggingSink
	{
		private WCFClientEventProvider ev;

		/// <summary>
		/// Creates new instance. 
		/// </summary>
		public AppFabricLoggingSink()
		{
			ev = new WCFClientEventProvider();
		}

		#region ILoggingSink Members

		/// <summary>
		/// Logs exception
		/// </summary>
		/// <param name="ex">The exception to log.</param>
		/// <returns></returns>
		public bool Log(Exception ex)
		{
			bool success = false;
			try
			{
				if (!Debugger.IsAttached)
				{
					Debugger.Launch();
				}
				ev.WriteErrorEvent(string.Format("Client Exception correlationID:'{0}'", Trace.CorrelationManager.ActivityId), ex.ToString());
				success = true;
			}
			catch
			{
				success = false;
			}
			return success;
		}

		#endregion
	}
}
